@extends('admin.public.admin')
@section('main')
<!-- 内容 -->
<div class="mws-panel grid_8">

		<div class="mws-panel-header">
			<span><i class="icon-table"></i> 商品列表</span>

		</div>
        <div class="mws-panel-body no-padding">
            	<div id="DataTables_Table_1_wrapper" class="dataTables_wrapper" role="grid">

               <form method="get" action="/admin/goods">

            		  <div id="DataTables_Table_1_length" class="dataTables_length">
               
              			<label>显示 
  	                        <select size="1" name="count" aria-controls="DataTables_Table_1">
  		                        <option value="10" @if(!empty($request['count']) && $request['count'] == 10) selected @endif>10</option>
  				        		<option value="20" @if(!empty($request['count']) && $request['count'] == 20) selected @endif>20</option>
  				        		<option value="30" @if(!empty($request['count']) && $request['count'] == 30) selected @endif>30</option>
  				        		<option value="50" @if(!empty($request['count']) && $request['count'] == 50) selected @endif>50</option>
  	                        </select> 
              			条
              			</label>
            		  </div>
			

            <div class="dataTables_filter" id="DataTables_Table_1_filter">
                    <label>
                    	搜索: <input type="text" name="search" value="{{$request['search'] or ''}}" aria-controls="DataTables_Table_1">
                            <input type="submit" class="btn btn-success btn-xs" value="搜索">
                    </label>
            </div>
            </form>
            <table class="mws-datatable-fn mws-table dataTable" id="DataTables_Table_1" aria-describedby="DataTables_Table_1_info">
                <thead>
                    <tr role="row">
		                    <th >ID</th>
		                    <th >所属分类</th>
		                    <th>商品名称</th>
		                    <th>商品标题</th>
                        <th>商品图片</th>
                        <th>库存</th>
                        <th>价格</th>
		                    <th>是否上架</th>
		                    <th>操作</th>
                    </tr>
                </thead>
                <?php 
                  $num=0;
                ?>
            <tbody role="alert" aria-live="polite" aria-relevant="all">
            @foreach($data as $k=>$v)
              
            		<tr>
                      <td class="">{{++$num}}</td>

					<?php

                     $len = substr_count($v['path'],',');
                     ?>
		
                    <td>{{str_repeat('|----',$len-1).$v['typename']}}</td>


                      <td class="">{{$v['goodsname']}}</td>

                      <td class="">{{$v['title']}}</td>

                     <td>
						<img width="200px" src="{{$v['img']}}" alt="">
						
					</td>

					<td class="">{{$v['num']}}</td>

					<td class="">{{$v['price']}}</td>


	             @if($v['is_on_sale'])
                       <td><span class="btn btn-success" onclick="sale(this,{{$v['id']}},1)">已上架</span></td>
            	 @else
            	       <td><span class="btn btn-danger" onclick="sale(this,{{$v['id']}},0)">未上架</span></td>

               @endif

                      <td align="center" width="100px">
                          <a href="/admin/goods/szy/{{$v['id']}}/{{$v['tid']}}"  class="icon-pencil-2" title="修改"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                          <a href="javascript:;" onclick="deletes(this,{{$v['id']}})"  class="icon-trash" title="删除"></a>
                      </td>

                    </tr>

              @endforeach
             </tbody>
           </table>

           <!-- 分页 -->
              <div id="page_page">
                   {!! $data->appends($request)->render()!!}
              </div>
           </div>
      </div>
</div>



<script>

    // ajax修改状态

  function sale(obj,id,is_on_sale){

    // 发送ajax请求

    if (is_on_sale) {
      // 发送ajax请求

      $.post('/admin/goods/ajaxSale',{id:id,"_token":"{{csrf_token()}}","is_on_sale":"0"},function(data){

        if (data==1) {
          $(obj).parent().html('<span class="btn btn-danger" onclick="sale(this,'+id+',0)">未上架</span>');
        }

      });
    }else{

      $.post('/admin/goods/ajaxSale',{id:id,"_token":"{{csrf_token()}}","is_on_sale":"1"},function(data){

        if (data==1) {
          $(obj).parent().html('<span class="btn btn-success" onclick="sale(this,'+id+',1)">已上架</span>');
        }

      });
    }
  }



// ajax删除

  function deletes(obj,id){
    // 发送ajax请求

    $.post("/admin/goods/"+id,{"_token":'{{csrf_token()}}',"_method":"delete"},function(data){
      // 判断是否成功

      if (data==1) {
        // 移除数据
          alert("删除成功");
        window.location.href = "/admin/goods";
      }else{
        alert('删除失败');
      }
    })
  }


</script>
	@endsection